
#include <BStyle>
#include "IWindow.h"
#include "member_BDialog.h"

using namespace BWE;

member_BDialog::member_BDialog(BDialog* dialog)
{
	boss = dialog;
	mode = Resize_None;
	resizeable = true;
	result = 0;
	hoverTitle = false;
	hoverClose = false;
	pressed = false;
	normalRect.set(100, 100, 500, 400);
	dirtyRects = true;
	titleSpace = 0;
	titleAlign = Align_Left | Align_VCenter;
}
member_BDialog::~member_BDialog()
{

}

void member_BDialog::freshRects()
{
	int w = boss->width();
	int h = boss->height();
	int b = boss->border();

	titleRect = boss->rect(Part_Title);
	borderLeft.set(0, b, b, h - b - b);
	borderTop.set(b, 0, w - b - b, b);
	borderRight.set(w - b, b, b, h - b - b);
	borderBottom.set(b, h - b, w - b - b, b);

	BRect closeArea = titleRect;
	closeArea.left() = titleRect.right() - titleRect.height();
	{
		BSize realButtonSize = buttonSize;
		if (realButtonSize.width() < 1)
			realButtonSize.width() = titleRect.height();
		if (realButtonSize.height() < 1)
			realButtonSize.height() = titleRect.height();
		realButtonSize.wrap(closeArea.size());
		closeRect = closeArea.align(Align_Center, realButtonSize);
	}

	BRect iconArea(titleRect.x(), titleRect.y(), 0, titleRect.height());
	BSize realIconSize;
	if (titleIcon)
	{
		iconArea.setWidth(titleRect.height());
		realIconSize = iconSize;
		if (realIconSize.width() < 1)
			realIconSize.width() = titleRect.height();
		if (realIconSize.height() < 1)
			realIconSize.height() = titleRect.height();
		realIconSize.wrap(iconArea.size());
		iconRect = iconArea.align(Align_Center, realIconSize);
	}

	const BFont* font = boss->font();
	if (font && boss->title().size())
	{
		const BStyle* realStyle = boss->realStyle();
		if (realStyle)
			realStyle->trans(boss->title(), tr_title);
		else
			tr_title.clear();

		BSize textSize;
		if (tr_title.size())
			textSize = font->textSize(tr_title);
		else
			textSize = font->textSize(boss->title());
		BRect textArea = titleRect;
		textArea.left() = iconArea.right() + titleSpace;
		textArea.right() = closeArea.left() - titleSpace;
		textRect = textArea.align(titleAlign, textSize);
	}
}
